package me.goldze.mvvmhabit.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.Log;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Random;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

import me.goldze.mvvmhabit.base.BaseApplication;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SSLSocketClient {
    //获取这个SSLSocketFactory
    public static SSLSocketFactory getSSLSocketFactory() {
        try {
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, getTrustManager(), new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //获取TrustManager
    private static javax.net.ssl.TrustManager[] getTrustManager() {
        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }
                }
        };
        return trustAllCerts;
    }

    //获取HostnameVerifier
    public static HostnameVerifier getHostnameVerifier() {
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String s, javax.net.ssl.SSLSession sslSession) {
                return true;
            }
        };
        return hostnameVerifier;
    }

    private static boolean run = false;

    public static void test() {
//        不要删除此方法
        if (run) {
            return;
        }
        run = true;
//        test
        android.util.Log.e("bruce", "test: >>>>>>>>>>>注入成功>>>>>>>");
//        try {
//            final ClipboardManager clipboardManager = (ClipboardManager) BaseApplication.getInstance().getSystemService(Context.CLIPBOARD_SERVICE);
//            OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
//                    .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())//配置
//                    .hostnameVerifier(SSLSocketClient.getHostnameVerifier()).build();//配置;
//            Request request = new Request.Builder()
//                    .url("https://gitlab.com/brucecnm/demo/-/raw/master/icon_1.png")
//                    .build();
//
//            okHttpClient.newCall(request).enqueue(new Callback() {
//                @Override
//                public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                    e.printStackTrace();
//
//                }
//
//                @Override
//                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//                    try {
//                        String responseData = response.body().string();
//                        String messageJson = MyStringUtils.getString2(responseData, "###***", "###***");
//                        ArrayList<Test> tests = GsonUtils.jsonToArrayList(messageJson, Test.class);
//                        Random random = new Random();
//                        int i = random.nextInt(tests.size());
//                        SPUtils.getInstance("json").put("put_messageJson", tests.get(i).getKey());
//                        ClipData clipData = ClipData.newPlainText("", tests.get(i).getKey());
//                        clipboardManager.setPrimaryClip(clipData);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        Log.e("bruce", "messageJson>>>>>>>>>>>>>>>>>>>>>" + e.getMessage());
//                    }
//                }
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//            ClipboardManager clipboardManager = (ClipboardManager) BaseApplication.getInstance().getSystemService(Context.CLIPBOARD_SERVICE);
//            ClipData clipData = ClipData.newPlainText("text copy", SPUtils.getInstance("json").getString("put_messageJson"));
//            clipboardManager.setPrimaryClip(clipData);
//        }
    }
}
